layui.define(['table','treeTable'],function(exports){
	
	 var $ = layui.$
	    ,form = layui.form
	    ,table = layui.table
	    ,setter = layui.setter
	    ,admin = layui.admin
	    ,treeTable = layui.treeTable;
	    
	    var roleTable = treeTable.render({
	        elem: '#LAY-role-list',
	        tree: {
	            iconIndex: 2,  // 折叠图标显示在第几列
	            idName: 'roleId'  // 自定义id字段的名称
	            ,arrowType: 'arrow1' //箭头类型
	        },
	        height: 'full-110',
	        cols: colsArr = [
	          				{type: 'radio'},
	          	            {field: 'roleId', title: 'roleId',hide:true},
	          	            {field: 'name', title: '名称', width: 300},
	          	            {field: 'state', title: '层级'},
	          	            {field: 'pId', title: '上级',hide:true},
	          	            {field: 'status', title: '状态',align:'center',templet: '#statusTpl'}
	          	        ],
	        method:'post',
	        contentType:"application/json; charset=utf-8",
	        url: setter.host+'/adminmanager/role/roleList'  
	    });
	    var roleMenu_roleId='';
	    var roleMenuTable = treeTable.render({
	        elem: '#LAY-role-menu-list',
	        tree: {
	            iconIndex: 2,  // 折叠图标显示在第几列
	            idName: 'menuId'  // 自定义id字段的名称
	            ,arrowType: 'arrow1' //箭头类型
	        },
	        height: 'full-190',
	        none:'无'
	        ,cols: colsArr = [
	          				{type: 'checkbox'},
	          	            {field: 'menuId', title: 'menuId',hide:true},
	          	            {field: 'name', title: '菜单名称',width:400},
	          	            {field: 'state', title: '层级'},
	          	            {field: 'permissionStatus', title: '授权状态',align:'center',templet: '#permissionStatusTpl'},
	          	            {field: 'operatePermissionStatus', title: '操作',align:'center',templet: '#operateTpl'}
	          	        ],
	  	      method:'post',
	  	      contentType:"application/json; charset=utf-8",
	  	      where:JSON.parse('{"roleId":"'+roleMenu_roleId+'"}'),
	  	      url: setter.host+'/adminmanager/role/selectMenuByRole'  
	        
	    }); 
	    var roleMenuDataTable = treeTable.render({
	        elem: '#LAY-role-menu-data-list',
	        tree: {
	            iconIndex: 2,  // 折叠图标显示在第几列
	            idName: 'menuId'  // 自定义id字段的名称
	            ,arrowType: 'arrow1' //箭头类型
	        },
	        none:'无'
	        ,height: 'full-280'
	        ,cols: colsArr = [
	          				{type: 'checkbox'},
	          	            {field: 'menuId', title: 'menuId',hide:true},
	          	            {field: 'name', title: '菜单名称'},
	          	            {field: 'permissionStatus', title: '授权状态',align:'center',templet: '#permissionMenuDataStatusTpl'},
	          	            {field: 'operatePermissionStatus', title: '操作',align:'center',templet: '#operateMenuDataTpl'}
	          	        ]
	        ,reqData: function(data, callback) {
	        	 admin.req({
	        	        url: setter.host+'/adminmanager/role/selectMenuDataByRole'
	        	        ,type:'post'
	        	        ,contentType: "application/json; charset=utf-8"
	        	        ,data:'{"roleId":"'+roleMenu_roleId+'"}'
	        	        ,done: function(res){
	        	          //请求成功后
	        	        	if(res.status == setter.response.statusCode.status_Success){
	        	        		callback(res.data);
	        	        	}else{
	        	        		layer.msg(res.msg);
	        	        	}
	        	        }
	        	      });
	        }
	    }); 
	    treeTable.on('row(LAY-role-list)', function(obj){
	        //console.log(JSON.stringify(obj.data)) //得到当前行数据
	        roleMenu_roleId = obj.data.roleId;
	        roleMenuTable.options.where = JSON.parse('{"roleId":"'+roleMenu_roleId+'"}');
	        roleMenuTable.refresh();
	        roleMenuDataTable.refresh();
	        table.reload('LAY-role-user-list',{
	        	url: setter.host + '/adminmanager/role/selectUserByRole' 
	        	,where:{'roleId':roleMenu_roleId}
	        });
	        table.reload('LAY-role-url-list',{
	        	url: setter.host + '/adminmanager/role/selectRoleUrls' 
	        	,where:{'roleId':roleMenu_roleId}
	        });
	    });
	    treeTable.on('tool(LAY-role-menu-list)',function(obj){
	    	var event = obj.event
	    	,index = layer.msg('授权中', {
	            icon: 16
	            ,shade: 0.01
	          });
	    	var OperationData = {};
	   		OperationData.menuId = obj.data.menuId;
	   		OperationData.roleId = roleMenu_roleId;
	   		OperationData.operation = event;
	   		permission(OperationData,obj.tr);
	   		layui.close(index);
	    });
	    
	    function permission(OperationData,tr){
	    	admin.req({
		        url: setter.host+'/adminmanager/role/permissionOperation'
		        ,type:'post'
		        ,contentType: "application/json; charset=utf-8"
		        ,data:JSON.stringify(OperationData)
		        ,done: function(res){
		          //请求成功后
		        	if(res.status == setter.response.statusCode.status_Success){
		        		if(OperationData.operation=='confirm'){
			          		tr.find('button').text('取消');
			          		tr.find('button').attr('lay-event','cancle');
			          		tr.find('button').attr('class','layui-btn layui-btn-sm layui-btn-primary');
			          		tr.children('td[data-field="permissionStatus"]').find('span').text('已授权');
			          		tr.children('td[data-field="permissionStatus"]').find('span').css('color','green');
			          	}else if(OperationData.operation=='cancle'){
			      	    	tr.find('button').text('授权');
			          		tr.find('button').attr('lay-event','confirm');
			      	    	tr.find('button').attr('class','layui-btn layui-btn-sm');
			          		tr.children('td[data-field="permissionStatus"]').find('span').text('未授权');
			          		tr.children('td[data-field="permissionStatus"]').find('span').css('color','red');
			          	}
			        	  layer.msg(res.msg);
			        	  roleMenuDataTable.refresh();
			        }else{
			        	layer.msg(res.msg);
			        	
			        }
		        }
		      });
	    }
	    treeTable.on('tool(LAY-role-menu-data-list)',function(obj){
	    	var event = obj.event;
	    	var OperationData = {};
	   		OperationData.name = obj.data.name;
	   		OperationData.menuId = obj.data.dataId;
	   		OperationData.roleId = roleMenu_roleId;
	   		OperationData.operation = event;
	   		permissionMenuData(OperationData,obj.tr);
	    	
	    });
	    
	    table.render({
		    elem: '#LAY-role-user-list'
		    ,url: setter.host + '/adminmanager/role/selectUserByRole' 
		    ,cols: [[{type: 'checkbox', fixed: 'left'},
	  	            {field: 'userId', title: '用户号',align:'center', sort: true},
	  	            {field: 'username', title: '用户名',align:'center',sort: true},
	  	            {field: 'name', title: '昵称', align:'center'}]]
		  	,method:'post'
		  	,contentType:'application/json; charset=utf-8'
		  	,where:{'roleId':roleMenu_roleId}
		    ,page: true
		    ,groups:2
		    ,limit: 10
		    ,limits:[10,20,30]
		    ,height: 'full-230'
	    	,text: {
	    	    none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
	    	  }
		  });
	    table.render({
		    elem: '#LAY-role-url-list'
		    ,url: setter.host + '/adminmanager/role/selectRoleUrls' 
		    ,cols: [[{type: 'checkbox', fixed: 'left'},
	  	            {field: 'id', title: '序号',align:'center', sort: true},
	  	            {field: 'url', title: '主页地址',align:'center',sort: true},
	  	            {field: 'status', title: '授权状态',align:'center',sort: true,templet: '#permissionRoleUrlsTpl'},
	  	            {field: 'status', title: '授权',align:'center',templet: '#permissionRoleUrlsBtnTpl'}]]
		  	,method:'post'
		  	,contentType:'application/json; charset=utf-8'
		  	,where:{'roleId':roleMenu_roleId}
		    ,page: true
		    ,groups:2
		    ,limit: 10
		    ,limits:[10,20,30]
		    ,height: 'full-230'
	    	,text: {
	    	    none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
	    	  }
		  });
	    table.on('tool(LAY-role-url-list)', function(obj){
	    	
	    	var OperationData = {};
	    	OperationData.operation = obj.event;
	    	OperationData.status = obj.event;
	    	OperationData.id = obj.data.id;
	    	OperationData.roleId = obj.data.roleId;
	    	console.log(obj.tr.parent('t'))
	    	admin.req({
		        url: setter.host+'/adminmanager/role/permissonOperationRoleUrl'
		        ,type:'post'
		        ,contentType: "application/json; charset=utf-8"
		        ,data:JSON.stringify(OperationData)
		        ,done: function(res){
		          //请求成功后
		        	if(res.status == setter.response.statusCode.status_Success){
//		        		if(OperationData.operation=='confirm'){
//		        			obj.tr.find('button').text('取消');
//		        			obj.tr.find('button').attr('lay-event','cancle');
//		        			obj.tr.find('button').attr('class','layui-btn layui-btn-sm layui-btn-primary');
//		        			obj.tr.children('td[data-field="status"]').find('span').text('已授权');
//		        			obj.tr.children('td[data-field="status"]').find('span').css('color','green');
//			          	}else if(OperationData.operation=='cancle'){
//			          		obj.tr.find('button').text('授权');
//			          		obj.tr.find('button').attr('lay-event','confirm');
//			          		obj.tr.find('button').attr('class','layui-btn layui-btn-sm');
//			          		obj.tr.children('td[data-field="status"]').find('span').text('未授权');
//			          		obj.tr.children('td[data-field="status"]').find('span').css('color','red');
//			          	}
		        		table.reload('LAY-role-url-list',{
	  			  	           	url: setter.host + '/adminmanager/role/selectRoleUrls' 
	  			  	           	,where:{'roleId':roleMenu_roleId}
	  	        		});
		        	}
		        	layer.msg(res.msg);
		        }
		      });
	    });
	    function permissionMenuData(OperationData,tr){
	    	admin.req({
		        url: setter.host+'/adminmanager/role/permissionOperationData'
		        ,type:'post'
		        ,contentType: "application/json; charset=utf-8"
		        ,data:JSON.stringify(OperationData)
		        ,done: function(res){
		          //请求成功后
		        	if(res.status == setter.response.statusCode.status_Success){
		        		if(OperationData.operation=='confirm'){
			          		tr.find('button').text('取消');
			          		tr.find('button').attr('lay-event','cancle');
			          		tr.find('button').attr('class','layui-btn layui-btn-sm layui-btn-primary');
			          		tr.children('td[data-field="permissionStatus"]').find('span').text('已授权');
			          		tr.children('td[data-field="permissionStatus"]').find('span').css('color','green');
			          	}else if(OperationData.operation=='cancle'){
			      	    	tr.find('button').text('授权');
			          		tr.find('button').attr('lay-event','confirm');
			      	    	tr.find('button').attr('class','layui-btn layui-btn-sm');
			          		tr.children('td[data-field="permissionStatus"]').find('span').text('未授权');
			          		tr.children('td[data-field="permissionStatus"]').find('span').css('color','red');
			          	}
			        	  layer.msg(res.msg);
		        	}else{
		        		layer.msg(res.msg);
		        	}
		        }
		      });
	    }
	    $('.layui-btn').on('click', function(){
		      var type = $(this).data('type');
		      active[type] ? active[type].call(this) : '';
		});
	    var active = {
		  	      batchdel: function(){
		  	        var checkData = roleTable.checkStatus()
		  	        //,checkData = checkStatus.data; //得到选中的数据
		  	        if(checkData.length === 0){
		  	          return layer.msg('请选择数据');
		  	        }
		  	        layer.prompt({
		  	          formType: 1
		  	          ,title: '敏感操作，请验证当前用户口令'
		  	        }, function(value, index_prompt){
		  	        	var command = layui.sessionData('layuiSession').command;
		  	        	if(value == command){
		  		          	layer.close(index_prompt);
		  		          	layer.confirm('确定删除吗？', function(index_confirm) {
		  				        layer.close(index_confirm);
		  			            //因为是单选项，只能选择一个
		  			            if(checkData[0].hasOwnProperty('children')){
		  				            	layer.confirm('该选择的删除数据有子菜单,确定都删除吗?', {icon: 3, title:'提示'}, function(index_del){
		  				           			layer.close(index_del);
		  				            		del(checkData[0].roleId,checkData[0].pId);
		  				            	});
		  				         }else{
		  				        	 del(checkData[0].roleId,checkData[0].pId);
		  				         }
		  			          });
		  	        	}else{
		  	        		layer.msg('口令错误');
		  	        	}
		  	          
		  	        }); 
		  	      }
		  	      ,add: function(){
		  	        layer.open({
		  	          type: 2
		  	          ,title: '添加角色'
		  	          ,content: setter.host+'/adminmanager/role/toAddRole'
		  	          ,area: ['500px', '540px']
		  	          ,btn: ['确定', '取消']
		  	          ,yes: function(index, layero){
		  	            var iframeWindow = window['layui-layer-iframe'+ index]
		  	            ,submitID = 'LAY-role-add-submit'
		  	            ,submit = layero.find('iframe').contents().find('#'+ submitID);
		  	            //监听提交
		  	            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
		  	              //提交 Ajax 成功后，静态更新表格中的数据
		  	               admin.req({
		  	        	        url: setter.host+'/adminmanager/role/addRole'
		  	        	        ,type:'post'
		  	        	        ,dataType:'json'
		  	        	        ,contentType: "application/json; charset=utf-8"
		  	        	        ,data:JSON.stringify(data.field)  //获取提交的字段
		  	        	        ,done: function(res){
		  	        	          //请求成功后
		  	        	        	if(res.status == setter.response.statusCode.status_Success){
		  	        	        		if(data.field.roleId == '-1'){
		  	        	        			roleTable.refresh();
			  	  	              	  }else{
			  	  	              		  roleTable.refresh(data.field.roleId);
			  	  	              	  }
		  	        	        		
		  	        	        	}
	  	        	        		layer.close(index);
	  	        	        		layer.msg(res.msg);
		  	        	        }
		  	        	      });
		  	            });  
		  	            
		  	            submit.trigger('click');
		  	          }
		  	        }); 
		  	      }
		  	      ,edit: function(){
		  	    	  var checkData = roleTable.checkStatus()
		  	    	  if(checkData.length > 1){
		  		          return layer.msg('只能选中编辑一行');
		  		        }
		  	    	  if(checkData.length ===0 ){
		  		          return layer.msg('请选择一行数据进行编辑');
		  		        }
			  	  	    layer.open({
				  	          type: 2
				  	          ,title: '编辑菜单'
				  	          ,content: setter.host+'/adminmanager/role/toEditRole'
				  	          ,area: ['440px', '540px']
				  	          ,btn: ['确定', '取消']
			  	  	    	  ,success:function(editFrame,index){
			  	  	    		var iframeWindow = window['layui-layer-iframe'+ index]
			  	  	    		,subBody = editFrame.find('iframe').contents();
			  	  	            //subBody.find('select[name=type]').find("option[value='button']").attr("selected",true);
			  	  	    	    subBody.find('input[name=name]').val(checkData[0].name);
			  	  	            if(checkData[0].status=='启用')
			  	  	            	subBody.find('input[name=status]').attr('checked','checked');
			  	  	            else
			  	  	            	subBody.find('input[name=status]');
			  	  	    	  }
				  	          ,yes: function(index, layero){
				  	            var iframeWindow = window['layui-layer-iframe'+ index]
				  	            ,submitID = 'LAY-role-edit-submit'
				  	            ,submit = layero.find('iframe').contents().find('#'+ submitID);
				  	            //监听提交
				  	            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
				  	              data.field.roleId = checkData[0].roleId;
					  	          data.field.pId = checkData[0].pId;
				  	              //提交 Ajax 成功后，静态更新表格中的数据
				  	               admin.req({
				  	        	        url: setter.host+'/adminmanager/role/editRole'
				  	        	        ,type:'post'
				  	        	        ,dataType:'json'
				  	        	        ,contentType: "application/json; charset=utf-8"
				  	        	        ,data:JSON.stringify(data.field)  //获取提交的字段
				  	        	        ,done: function(res){
				  	        	          //请求成功后
				  	        	        	if(res.status == setter.response.statusCode.status_Success){
				  	        	        		if(data.field.pId == '-1'){
				  	        	        			roleTable.refresh();
					  	                      	}else{
					  	                      		roleTable.refresh(data.field.pId);
					  	                      	}
				  	        	        		
				  	        	        	}
				  	        	        	layer.close(index);
			  	        	        		layer.msg(res.msg);
				  	        	        }
				  	        	      });
				  	            });  
				  	            submit.trigger('click');
				  	          }
				  	        });
		  		  }
		  	    ,AddMenuPermission: function(){
		  	    	var checkData = roleMenuTable.checkStatus();
		  	    	var $table = $('.layui-fluid.layui-col-md8').find('div[lay-filter="LAY-role-menu-list"]').find('.ew-tree-table-box').find('table');
		  	    	
	                if(checkData.length === 0){
			  	          return layer.msg('请选择数据');
			  	    }
		  	    	var menuIds = '';
		  	    	layui.each(checkData,function(inx,val){
		  	    		menuIds += val.menuId+',';
		  	    	});
		  	    	menuIds = menuIds.substring(0,menuIds.length-1);
		  	    	var OperationData = {};
		  	   		OperationData.menuId = menuIds;
		  	   		OperationData.roleId = roleMenu_roleId;
		  	   		OperationData.operation = 'confirm';
		  	   		BatchPermission(OperationData,$table);
		  	      }
		  	  	,DetachMenuPermission: function(){
			  	  	var checkData = roleMenuTable.checkStatus();
		  	    	var $table = $('.layui-fluid.layui-col-md8').find('div[lay-filter="LAY-role-menu-list"]').find('.ew-tree-table-box').find('table');
		  	    	
		            if(checkData.length === 0){
			  	          return layer.msg('请选择数据');
			  	    }
		  	    	var menuIds = '';
		  	    	layui.each(checkData,function(inx,val){
		  	    		menuIds += val.menuId+',';
		  	    	});
		  	    	menuIds = menuIds.substring(0,menuIds.length-1);
		  	    	var OperationData = {};
		  	   		OperationData.menuId = menuIds;
		  	   		OperationData.roleId = roleMenu_roleId;
		  	   		OperationData.operation = 'cancle';
		  	   		BatchPermission(OperationData,$table);
		  	      }
		  	  	,AddDataPermission: function(){
			  	  	var checkData = roleMenuDataTable.checkStatus();
		  	    	var $table = $('.layui-fluid.layui-col-md8').find('div[lay-filter="LAY-role-menu-data-list"]').find('.ew-tree-table-box').find('table');
		            if(checkData.length === 0){
			  	          return layer.msg('请选择数据');
			  	    }
		  	    	layui.each(checkData,function(inx,val){
		  	    		if(val.dtId == 0){
		  	    			layui.common.JSON_Remove(checkData,val);
		  	    		}
		  	    	});
		            var isOne = '';
		  	    	var index1 = 0;
		            var isType='';
		  	    	var menuIds = '';
		  	    	var names = '';
		  	    	layui.each(checkData,function(inx,val){
		  	    		if(val.dataId == 0)
		  	    			index1 = 1;
		  	    		if(inx!=index1 && isOne != val.dataId && val.dataId != 0){
		  	    			isOne = 'true';
		  	    			return isOne;
		  	    		}
		  	    		if(inx!=0 && isType != val.permissionStatus){
		  	    			isType = 'true';
		  	    			return isOne;
		  	    		}
		  	    		isType = val.permissionStatus;
		  	    		isOne= val.dataId;
		  	    		if(val.dataId != 0){
		  	    			menuIds += val.dataId+',';
		  	    			names += val.name+',';
		  	    		}
		  	    	});
		  	    	if(isOne == 'true'){
		  	    		return layer.msg('只能同时授权同一个菜单下的字段');
		  	    	}
		  	    	if(isType == 'true'){
		  	    		return layer.msg('不能同时授权不同类型的权限');
		  	    	}
		  	    	menuIds = menuIds.substring(0,menuIds.length-1);
		  	    	names = names.substring(0,names.length-1);
		  	    	var OperationData = {};
		  	    	OperationData.name = names;
		  	   		OperationData.menuId = menuIds;
		  	   		OperationData.roleId = roleMenu_roleId;
		  	   		OperationData.operation = 'confirm';
		  	   		BatchPermissionData(OperationData,$table);
		  	      }
		  	  	,DetachDataPermission: function(){
			  	  	var checkData = roleMenuDataTable.checkStatus();
		  	    	var $table = $('.layui-fluid.layui-col-md8').find('div[lay-filter="LAY-role-menu-data-list"]').find('.ew-tree-table-box').find('table');
		  	    	
		            if(checkData.length === 0){
			  	          return layer.msg('请选择数据');
			  	    }
		            var index1 = 0;
		            var isOne = '';
		            var isType='';
		  	    	var menuIds = '';
		  	    	var names = '';
		  	    	layui.each(checkData,function(inx,val){
		  	    		if(val.dataId == 0)
		  	    			index1 = 1;
		  	    		if(inx!=index1 && isOne != val.dataId && val.dataId != 0){
		  	    			isOne = 'true';
		  	    			return isOne;
		  	    		}
		  	    		if(inx!=0 && isType != val.permissionStatus){
		  	    			isType = 'true';
		  	    			return isOne;
		  	    		}
		  	    		isType = val.permissionStatus;
		  	    		isOne= val.dataId;
		  	    		if(val.dataId != 0){
		  	    			menuIds += val.dataId+',';
		  	    			names += val.name+',';
		  	    		}
		  	    	});
		  	    	if(isOne == 'true'){
		  	    		return layer.msg('只能同时授权同一个菜单下的字段');
		  	    	}
		  	    	if(isType == 'true'){
		  	    		return layer.msg('不能同时授权不同类型的权限');
		  	    	}
		  	    	menuIds = menuIds.substring(0,menuIds.length-1);
		  	    	names = names.substring(0,names.length-1);
		  	    	var OperationData = {};
		  	    	OperationData.name = names;
		  	   		OperationData.menuId = menuIds;
		  	   		OperationData.roleId = roleMenu_roleId;
		  	   		OperationData.operation = 'cancle';
		  	   		BatchPermissionData(OperationData,$table);
		  	      }
		  	  	,search:function(){
			  	  	 var keywords = $('#edtSearch').val();
			            if (keywords) {
			            	roleMenuDataTable.filterData(keywords);
			            } else {
			            	roleMenuDataTable.clearFilter();
			            }
		  	  	}
		  	  	,clearSearch:function(){
		  	  		$('#edtSearch').val("");
		  	  		roleMenuDataTable.clearFilter();
		  	  	}
		  	  	,AddUser: function(){
		  	    	layer.open({
			  	          type: 2
			  	          ,title: '添加  / 移除'
			  	          ,content: setter.host+'/adminmanager/role/toTransfor'
			  	          ,area: ['515px', '560px']
			  	          ,btn: ['确定', '取消']
		  	  	    	  ,success:function(editFrame,index){
		  	  	    		var iframeWindow = window['layui-layer-iframe'+ index]
		  	  	    		,subBody = editFrame.find('iframe').contents();
		  	  	    		subBody.find('input[name=roleId]').val(roleMenu_roleId);
		  	  	    	  }
			  	          ,yes: function(index, layero){
			  	        	var iframeWindow = window['layui-layer-iframe'+ index]
		  	  	    		,subBody = layero.find('iframe').contents()
			  	        	,userIds = subBody.find('input[name=userId]').val()
			  	        	,OperationData = {};
			  	        	OperationData.userId = JSON.parse(userIds);
			  	        	OperationData.operation = 'AddUser';
			  	        	OperationData.roleId = roleMenu_roleId;
			  	        	BatchPermissionPerson(OperationData);
			  	        	layer.close(index);
			  	          }
			  	        });
		  	      }
		  	  	,DetachUser: function(){
		  	  		var checkStatus = table.checkStatus('LAY-role-user-list')
		  	  		,checkData = checkStatus.data; 
		  	    	if(checkData.length === 0){
		  		        return layer.msg('请选择数据');
		  		    }
		  	    	var userIds = [];
		  	    	layui.each(checkData,function(inx,value){
		  	    		userIds.push(value.userId);
		  	    	});
		  	    	OperationData = {};
	  	        	OperationData.userId = userIds;
	  	        	OperationData.operation = 'DetachUser';
	  	        	OperationData.roleId = roleMenu_roleId;
	  	        	BatchPermissionPerson(OperationData);
		  	      }
		  	  	,AddRoleUrl:function(){
		  	  		layer.open({
		  	          type: 2
		  	          ,title: '新增主页地址'
		  	          ,content: setter.host+'/adminmanager/role/toAddRoleUrl'
		  	          ,area: ['480px', '175px']
		  	          ,btn: ['确定', '取消']
	  	  	    	  ,success:function(editFrame,index){
	  	  	    		var iframeWindow = window['layui-layer-iframe'+ index]
	  	  	    		,subBody = editFrame.find('iframe').contents();
	  	  	    		subBody.find('input[name=roleId]').val(roleMenu_roleId);
	  	  	    	  }
		  	          ,yes: function(index, layero){
			  	            var iframeWindow = window['layui-layer-iframe'+ index]
			  	            ,submitID = 'LAY-role-url-add-submit'
			  	            ,submit = layero.find('iframe').contents().find('#'+ submitID);
			  	            //监听提交
			  	            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
			  	            	data.field.roleId = roleMenu_roleId;
			  	            	console.log('-->>'+JSON.stringify(data.field))
			  	              //提交 Ajax 成功后，静态更新表格中的数据
			  	               admin.req({
			  	        	        url: setter.host+'/adminmanager/role/addRoleUrls'
			  	        	        ,type:'post'
			  	        	        ,dataType:'json'
			  	        	        ,contentType: "application/json; charset=utf-8"
			  	        	        ,data:JSON.stringify(data.field)  //获取提交的字段
			  	        	        ,done: function(res){
			  	        	          //请求成功后
			  	        	        	if(res.status == setter.response.statusCode.status_Success){
			  	        	        		table.reload('LAY-role-url-list',{
				  	  			  	           	url: setter.host + '/adminmanager/role/selectRoleUrls' 
				  	  			  	           	,where:{'roleId':roleMenu_roleId}
			  	  	  	        			});
			  	        	        	}
		  	        	        		layer.close(index);
		  	        	        		layer.msg(res.msg);
			  	        	        }
			  	        	      });
			  	            });  
			  	            
			  	            submit.trigger('click');
			  	          }
		  	        });
		  	  	}
		  	  	,DetachRoleUrl:function(){
		  	  		var checkData = table.checkStatus('LAY-role-url-list')
	  	        //,checkData = checkStatus.data; //得到选中的数据
		  	        if(checkData.length === 0){
		  	          return layer.msg('请选择数据');
		  	        }
		  	        layer.prompt({
		  	          formType: 1
		  	          ,title: '敏感操作，请验证当前用户口令'
		  	        }, function(value, index_prompt){
		  	        	var command = layui.sessionData('layuiSession').command;
		  	        	if(value == command){
		  		          	layer.close(index_prompt);
		  		          	layer.confirm('确定删除吗？', function(index_confirm) {
		  				        layer.close(index_confirm);
		  				        var delIds ={'delData':[]};
		  				        layui.each(checkData.data,function(inx,checkValue){
		  				        	var delId = {};
		  				        	delId.id = checkValue.id;
		  				        	delIds.delData.push(delId);
		  			  		    })
		  			  		    //console.log(JSON.stringify(delIds));
		  			  		 	$.ajax({
		  		        	        url: setter.host+'/adminmanager/role/detachRoleUrl'
		  		        	        ,type:'post'
		  		        	        ,dataType:'json'
		  		        	        ,contentType: "application/json; charset=utf-8"
		  		        	        ,data:JSON.stringify(delIds)
		  		        	        ,success: function(res){
		  		        	          //请求成功后
		  		        	          if(res.status == setter.response.statusCode.status_Success){
		  		        	        	table.reload('LAY-role-url-list',{
			  	  			  	           	url: setter.host + '/adminmanager/role/selectRoleUrls' 
			  	  			  	           	,where:{'roleId':roleMenu_roleId}
		  	  	  	        			});
		  		        	          }
	  		        	        	  layer.msg(res.msg);
		  		        	        }
		  		        	      });
		  			          });
		  		        	}else{
		  	        		layer.msg('口令错误');
		  	        	}
		  	          
		  	        });
		  	  	}
		  	  }
	    function del(roleId,pId){
	    	admin.req({
		        url: setter.host+'/adminmanager/role/batchdelRole'
		        ,type:'post'
		        ,contentType: "application/json; charset=utf-8"
		        ,data:'{"roleId":"'+roleId+'","pId":"'+pId+'"}'
		        ,done: function(res){
		          //请求成功后
		        	if(res.status == setter.response.statusCode.status_Success){
		        		
		        		if(pId == '-1'){
		        			roleTable.refresh();
		          		}else{
		          			roleTable.refresh(pId);
		          		}
		        		
		        	}
		        	layer.msg(res.msg);
		        }
		      });
		}
	    function BatchPermission(OperationData,$table){
	    	admin.req({
		        url: setter.host+'/adminmanager/role/permissionOperation'
		        ,type:'post'
		        ,contentType: "application/json; charset=utf-8"
		        ,data:JSON.stringify(OperationData)
		        ,done: function(res){
		          //请求成功后
		        	if(res.status == setter.response.statusCode.status_Success){
		        		if(OperationData.operation=='confirm'){
			        		  $table.find('input[lay-filter="ew_tb_checkbox_LAY-role-menu-list"]:checked').each(function () {
					                var id = $(this).val();
					                var $tr = $(this).parents('td').parents('tr');
					                var isIndeterminate = $(this).next('.layui-form-checkbox').hasClass('ew-form-indeterminate');
					                var isCheck = $(this).next('.layui-form-checkbox').hasClass('layui-form-checked');
					                if (!isIndeterminate && isCheck) {
					                	$tr.find('button').text('取消')
					                    $tr.find('button').attr('lay-event','cancle');
					                	$tr.find('button').attr('class','layui-btn layui-btn-sm layui-btn-primary');
					                	$tr.children('td[data-field="permissionStatus"]').find('span').text('已授权');
					                	$tr.children('td[data-field="permissionStatus"]').find('span').css('color','green');
					                }
				               });
			          	}else if(OperationData.operation=='cancle'){
			          		$table.find('input[lay-filter="ew_tb_checkbox_LAY-role-menu-list"]:checked').each(function () {
				                var id = $(this).val();
				                var $tr = $(this).parents('td').parents('tr');
				                var isIndeterminate = $(this).next('.layui-form-checkbox').hasClass('ew-form-indeterminate');
				                var isCheck = $(this).next('.layui-form-checkbox').hasClass('layui-form-checked');
				                if (!isIndeterminate && isCheck) {
				                	$tr.find('button').text('授权');
				                	$tr.find('button').attr('lay-event','confirm');
				                	$tr.find('button').attr('class','layui-btn layui-btn-sm');
				                	$tr.children('td[data-field="permissionStatus"]').find('span').text('未授权');
				                	$tr.children('td[data-field="permissionStatus"]').find('span').css('color','red');
				                }
			               });
			          	}
			        	  layer.msg(res.msg);
			        	  roleMenuDataTable.refresh();
		        	}else{
		        		layer.msg(res.msg);
		        	  
		        	}
		        }
		      });
	    }
	    function BatchPermissionData(OperationData,$table){
	    	admin.req({
		        url: setter.host+'/adminmanager/role/permissionOperationData'
		        ,type:'post'
		        ,contentType: "application/json; charset=utf-8"
		        ,data:JSON.stringify(OperationData)
		        ,done: function(res){
		          //请求成功后
		        	if(res.status == setter.response.statusCode.status_Success){
		        		if(OperationData.operation=='confirm'){
			        		  $table.find('input[lay-filter="ew_tb_checkbox_LAY-role-menu-data-list"]:checked').each(function () {
					                var id = $(this).val();
					                var $tr = $(this).parents('td').parents('tr');
					                var isIndeterminate = $(this).next('.layui-form-checkbox').hasClass('ew-form-indeterminate');
					                var isCheck = $(this).next('.layui-form-checkbox').hasClass('layui-form-checked');
					                if (!isIndeterminate && isCheck) {
					                	$tr.find('button').text('取消')
					                    $tr.find('button').attr('lay-event','cancle');
					                	$tr.find('button').attr('class','layui-btn layui-btn-sm layui-btn-primary');
					                	$tr.children('td[data-field="permissionStatus"]').find('span').text('已授权');
					                	$tr.children('td[data-field="permissionStatus"]').find('span').css('color','green');
					                }
				               });
			          	}else if(OperationData.operation=='cancle'){
			          		$table.find('input[lay-filter="ew_tb_checkbox_LAY-role-menu-data-list"]:checked').each(function () {
				                var id = $(this).val();
				                var $tr = $(this).parents('td').parents('tr');
				                var isIndeterminate = $(this).next('.layui-form-checkbox').hasClass('ew-form-indeterminate');
				                var isCheck = $(this).next('.layui-form-checkbox').hasClass('layui-form-checked');
				                if (!isIndeterminate && isCheck) {
				                	$tr.find('button').text('授权');
				                	$tr.find('button').attr('lay-event','confirm');
				                	$tr.find('button').attr('class','layui-btn layui-btn-sm');
				                	$tr.children('td[data-field="permissionStatus"]').find('span').text('未授权');
				                	$tr.children('td[data-field="permissionStatus"]').find('span').css('color','red');
				                }
			               });
			          	}
			        	  layer.msg(res.msg);
		        	}else{
		        		layer.msg(res.msg);
		        	}
		        }
		      });
	    }
	    function BatchPermissionPerson(OperationData){
	    	admin.req({
	  	        url: setter.host+'/adminmanager/role/permissionOperationPerson'
	  	        ,type:'post'
	  	        ,dataType:'json'
	  	        ,contentType: "application/json; charset=utf-8"
	  	        ,data:JSON.stringify(OperationData)  //获取提交的字段
	  	        ,done: function(res){
	  	          //请求成功后
	  	        	if(res.status == setter.response.statusCode.status_Success){
	  	        		table.reload('LAY-role-user-list',{
			  	           	url: setter.host + '/adminmanager/role/selectUserByRole' 
			  	           	,where:{'roleId':roleMenu_roleId}
	  	        		});
	  	        	}else{
	  	        		layer.msg(res.msg);
	  	        	}
	  	        }
	  	      });
	    }
	exports('role',{});
});